package ui;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;

import midlet.VadeMecumMIDlet;
import multimedia.SaveSnapshotThread;
import multimedia.SendToServiceWithContextThread;
import multimedia.SendToServiceWithoutContextThread;

public class VadeMecumVideoViewer extends Canvas implements PlayerListener {

	private byte[] videoArray;
	private Player player;
	private VideoControl videoControl;
	public Command cmdRepeat;
	public Command play;
	public Command repeat;
	public Command stop;
	public Command cmdContinue;
	public Command cmdBack;
	public Command save;
	public Command sendToServiceWhithoutContext;
	public Command sendToServiceWithContext;
	public VadeMecumMIDlet midlet;
	private boolean isPlaying = false;
	
	public VadeMecumVideoViewer(VadeMecumMIDlet midlet, byte[] videoArray) {
		this.videoArray = videoArray;
		this.midlet = midlet;
		
		this.play = new Command("Play", Command.OK, 1);
		this.stop = new Command("Stop", Command.OK, 1);
		this.repeat = new Command("Repeat", Command.OK, 1);
		this.cmdRepeat   = new Command("Repeat", Command.SCREEN, 1);
		this.cmdContinue = new Command("Resume", Command.OK, 1);
		this.cmdBack = new Command("Back", Command.BACK, 0);
		
		save = new Command("Save video", Command.OK, 0);
		sendToServiceWhithoutContext = new Command("Send without context", Command.OK, 0);
		sendToServiceWithContext = new Command("Send with context", Command.OK, 0);
		
		this.addCommand(this.play);		
		this.addCommand(save);
		this.addCommand(sendToServiceWhithoutContext);
		this.addCommand(sendToServiceWithContext);
		this.addCommand(cmdBack);
		
		int width  = this.getWidth();
		int height = this.getHeight();
		
		try{			
			InputStream in = new ByteArrayInputStream(this.videoArray);
			this.player = Manager.createPlayer(in, "video/mp4");
			this.player.realize();
			this.player.addPlayerListener(this);
			this.videoControl = (VideoControl) this.player.getControl("VideoControl");
			this.videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
			this.videoControl.setDisplayLocation(0, 0);
			this.videoControl.setDisplaySize(width, height);
			this.videoControl.setDisplayFullScreen(true);
			this.videoControl.setVisible(true);
		} catch (IOException e) {
			midlet.error(e);
		} catch (MediaException e) {
			midlet.error(e);
		}
		
		this.setTitle("Viewing video");
	}
	
	protected void paint(Graphics arg0) {}
	
	public void repeat() {
		play();
	}
	
	public void play() {		
		try {
			this.player.start();
			this.addCommand(cmdContinue);
			this.addCommand(stop);			
			this.removeCommand(play);			
			this.isPlaying = true;	
		} catch (MediaException me) {
			midlet.error(me);
		}
	}

	public void closeStreams() {
		stop();
		this.player.close();		
	}
	
	public void stop() {
		if (this.isPlaying) {
			try {
				this.player.stop();
				this.isPlaying = false;
			} catch (MediaException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void saveSnapshot() {
		Thread saveThread = new Thread(new SaveSnapshotThread(midlet, this, videoArray, System.currentTimeMillis()));
		saveThread.start();
	}

	public void sendWithoutContext() {
		Thread sendThread = new Thread(new SendToServiceWithoutContextThread(midlet, this, videoArray, System.currentTimeMillis()));
		sendThread.start();
	}

	public void sendWithContext() {
		Thread sendThread = new Thread(new SendToServiceWithContextThread(midlet, this, videoArray, System.currentTimeMillis()));
		sendThread.start();
	}

	public void playerUpdate(Player player, String event, Object data) {
		if (event.equals(PlayerListener.END_OF_MEDIA)) {
			this.removeCommand(stop);
			this.removeCommand(cmdContinue);
			this.addCommand(repeat);
		} else if (PlayerListener.STOPPED.equals(event)) {
			this.removeCommand(stop);
			this.removeCommand(cmdContinue);
			this.addCommand(repeat);
		}
	}

}
